package com.ruoyi.web.controller.message;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.message.domain.LeaveMessage;
import com.ruoyi.message.service.ILeaveMessageService;
import io.jsonwebtoken.lang.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.AjaxResult;

import java.util.List;

import static com.ruoyi.common.utils.PageUtils.startPage;

@RestController
@RequestMapping("/api/message")
public class ApiLeaveMessageController extends BaseController {
    @Autowired
    private ILeaveMessageService leaveMessageService;

    /**
     * 留言列表
     * @return
     */
    @PostMapping("/list")
    public TableDataInfo list()
    {
        startPage();
        LeaveMessage leaveMessage = new LeaveMessage();
        leaveMessage.setStatus(1);
        List<LeaveMessage> list = leaveMessageService.selectLeaveMessageList(leaveMessage);
        return getDataTable(list);
    }

    /**
     * 新增留言
     * @param leaveMessage 留言实体类
     * @return 新增结果
     */
    @PostMapping("/insertMessage")
    public AjaxResult insertMessage(@RequestBody LeaveMessage leaveMessage)
    {
        Assert.isTrue(StringUtils.isNotEmpty(leaveMessage.getName()), "留言人不能为空");
        Assert.isTrue(StringUtils.isNotEmpty(leaveMessage.getContent()), "留言内容不能为空");
        leaveMessage.setStatus(0);
        leaveMessage.setCreateTime(DateUtils.getNowDate());
        int i = leaveMessageService.insertLeaveMessage(leaveMessage);
        return toAjax(i);
    }
}
